package com.stedu.service.impl;

import com.stedu.bean.Student;
import com.stedu.exception.SteduException;
import com.stedu.mapper.StudentMapper;
import com.stedu.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public List<Student> selectBySname(String sname,String sgender) {
        return studentMapper.selectBySname(sname,sgender);
    }

    @Override
    public boolean insert(Student s) throws SteduException {
        if (studentMapper.selectBySname(s.getSname(),s.getSgender())==null){
            //抛出自定义异常
            throw new SteduException("该学生已经存在，无法重复添加");
        }
        return studentMapper.insert(s)==1;
    }

    @Override
    public boolean delete(Integer sid) throws SteduException {
        if (studentMapper.selectBySid(sid)==null) {
            //抛出自定义异常
            throw new SteduException("该学生信息不存在");
        }
        return studentMapper.delete(sid)==1;
    }

    @Override
    public boolean update(Student e) throws SteduException {

        return studentMapper.update(e)==1;
    }

    @Override
    public List<Student> selectAll() {
        return studentMapper.selectAll();
    }

    @Override
    public Student selectBySid(Integer sid) {
        return studentMapper.selectBySid(sid);
    }
}
